<?php namespace Summer\Box\Controllers;

use Backend\Classes\Controller;
use BackendMenu;
use Summer\Box\Models\BoxItemModel;
use Summer\Box\Models\BoxOrderModel;
use Summer\Hjd\Models\BonusRuleModel;
use Summer\Shop\Models\ExpressModel;
use Input;
use Config;
use Db;
use Cache;
use Carbon\Carbon;
use Flash;
use ApplicationException;
use Redirect;

class BoxOrder extends Controller
{
    public $implement = [        'Backend\Behaviors\ListController',        'Backend\Behaviors\FormController'    ];
    
    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('Summer.Box', 'main-menu-item-box', 'side-menu-item-order');
    }

    /**
     * 审核
     */
    public function onShenhe(){
        $data=Input::all();
        $record_id=$data['record_id'];
        $status_sh=$data['status_sh'];
        try {
            $order=BoxOrderModel::where('id',$record_id)->where('status',3)->where('status_slug','shenhe_shangjia')->first();
            if(!$order){
                throw new ApplicationException('记录不存在或不能操作');
            }
            $item=BoxItemModel::where('id',$order->item_id)->where('status',1)->first();
            if(!$item){
                throw new ApplicationException('盲盒信息获取失败');
            }
            if($status_sh==1){//通过
                //缓存解锁
                $item_cache_key='box_item_'.$item->id;
                Cache::put($item_cache_key, 'unlock');
                //改变订单状态
                $order->status=4;
                $order->status_slug='shangjia_ing';
                $order->save();
                //改变盲盒状态
                $item->is_shangjia=1;
                $item->price=$order->sell_money;//涨价
                $item->save();
                //判断拆分
                $bc_chaifen=BonusRuleModel::where('slug','chaifen')->first();
                if($item->type==1){//上午场
                    $chaifen_money=$bc_chaifen->rule_value;
                }else{//下午场
                    $chaifen_money=$bc_chaifen->rule_a;
                }
                if($item->price>=$chaifen_money){
                    //需要拆分 处理订单-盲盒-拆分盲盒
                    //处理订单
                    $order->status_slug='split';
                    $order->save();
                    //处理盲盒
                    $item->status=-1;
                    $item->is_shangjia=0;
                    $item->status_slug='split';
                    $item->save();
                    //拆分盲盒
                    $item1=new BoxItemModel();
                    $item1->title=$item->title;
                    $item1->cover=$item->cover;
                    $item1->type=$item->type;
                    $item1->price=$item->price*0.5;
                    $item1->user_id=$item->user_id;
                    $item1->from=2;
                    $item1->sale=0;
                    $item1->status=1;
                    $item1->status_slug='normal';
                    $item1->is_shangjia=1;
                    $item1->pid=$item->id;
                    $item1->save();
                    $item2=new BoxItemModel();
                    $item2->title=$item->title;
                    $item2->cover=$item->cover;
                    $item2->type=$item->type;
                    $item2->price=$item->price*0.5;
                    $item2->user_id=$item->user_id;
                    $item2->from=2;
                    $item2->sale=0;
                    $item2->status=1;
                    $item2->status_slug='normal';
                    $item2->is_shangjia=1;
                    $item2->pid=$item->id;
                    $item2->save();
                }
            }else{//拒绝
                //改变订单状态
                $order->status=-1;
                $order->status_slug='disable';
                $order->save();
            }
            Flash::success('操作成功!');
        }
        catch (ApplicationException $e) {
            Flash::error($e->getMessage());
        }
        return Redirect::refresh();
    }

    public function onListBtnPromptForm(){
        $listBtnParams=Input::all();
        $config_path=$listBtnParams['config_path']??'$/summer/hjd/config/list_prompt_btn_fields.yaml';
        $widgetConfig = $this->makeConfig($config_path);
        $widgetConfig->model=new ExpressModel();
        $listBtnPromptFormWidget=$this->makeWidget('Backend\Widgets\Form', $widgetConfig);
        $this->vars['listBtnPromptFormWidget'] = $listBtnPromptFormWidget;
        $this->vars['listBtnParams'] = $listBtnParams;
        return $this->makePartial('$/summer/hub/partials/_list_btn_prompt.htm');
    }
}
